/**
 * Created by Ysssssss on 21/3/3.
 */

import { put, takeEvery, select } from 'redux-saga/effects'
import { getAxios } from "@utils/api";
import apiType, {
    SET_HEADER_BUTTON_INFO,
    SET_HEADER_BREAD_CRUMB_INFO,
    GET_AREA_JSON, GET_STORE_SELECTION
} from "@constants/ActionTypes";

export const setHeaderButtonInfo = (buttonInfo) => ({
    type: SET_HEADER_BUTTON_INFO,
    payload: {
        buttonInfo
    }
})

export const setHeaderBreadCrumbInfo = (breadcrumbInfo) => ({
    type: SET_HEADER_BREAD_CRUMB_INFO,
    payload: {
        breadcrumbInfo
    }
})

export const getAreaJson = () => ({
    type: apiType(GET_AREA_JSON).request
})


const sagas = {
    * [apiType(GET_AREA_JSON).request]() {
        try {
            const { cascaderList } = yield select(state => state.common)
            if (cascaderList && cascaderList.length) return ;
            const response = yield getAxios('https://bgapi.bilinbili.com/area.json')
            yield put({
                type: apiType(GET_AREA_JSON).success,
                response,
            });
            console.log('response', response)
        } catch (e) {
            yield put({type: apiType(GET_AREA_JSON).failure});
        }
    },
}

export default Object.keys(sagas).map(item => {
    return function * s() {
        yield takeEvery(item, function *(args) {
            try {
                yield sagas[item](args)
            } catch (e) {
                console.log(e)
            }
        })
    }
})